package com.anbow.courseware.service.impl;

import com.anbow.courseware.entity.DO.TeachingQuestions;
import com.anbow.courseware.entity.DTO.TeachingQuestionsDTO;
import com.anbow.courseware.mapper.TeachingQuestionsMapper;
import com.anbow.courseware.mapper.VipTeachingQuestionsMapper;
import com.anbow.courseware.service.TeachingQuestionsService;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhaoYuFei
 */
@Service
public class TeachingQuestionsServiceImpl implements TeachingQuestionsService {
    @Autowired
    private TeachingQuestionsMapper teachingQuestionsMapper;

    @Override
    public List<TeachingQuestionsDTO> getAll() {
        List<TeachingQuestions> all = teachingQuestionsMapper.getAll();
        if (all == null || all.size() == 0) {
            return null;
        } else {
            return all.stream().map(e -> {
                TeachingQuestionsDTO teachingQuestionsDTO = new TeachingQuestionsDTO();
                BeanUtils.copyProperties(e, teachingQuestionsDTO);
                return teachingQuestionsDTO;
            }).collect(Collectors.toList());
        }
    }

    @Override
    public List<TeachingQuestionsDTO> getByLabel(String label,Integer pageNum) {
        if (pageNum==null||"null".equals(pageNum)){
            pageNum=0;
        }else {
            pageNum= (pageNum-1)*10;
        }
        List<TeachingQuestions> list = teachingQuestionsMapper.getByLabel(label,pageNum);
        if (list == null) {
            return null;
        }
        return list.stream().map(e -> {
            TeachingQuestionsDTO teachingQuestionsDTO = new TeachingQuestionsDTO();
            BeanUtils.copyProperties(e, teachingQuestionsDTO);
            return teachingQuestionsDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public TeachingQuestionsDTO getById(String id) {
        TeachingQuestions t = teachingQuestionsMapper.getById(id);
        TeachingQuestionsDTO teachingQuestionsDTO = new TeachingQuestionsDTO();
        BeanUtils.copyProperties(t, teachingQuestionsDTO);
        return teachingQuestionsDTO;
    }

    @Override
    public void update(TeachingQuestionsDTO teachingQuestionsDTO) {
        TeachingQuestions teachingQuestions = new TeachingQuestions();
        BeanUtils.copyProperties(teachingQuestionsDTO, teachingQuestions);
        teachingQuestionsMapper.update(teachingQuestions);
    }

    @Override
    public void delete(String id) {
        teachingQuestionsMapper.delete(id);
    }

    @Override
    public void add(TeachingQuestionsDTO teachingQuestionsDTO) {
        TeachingQuestions teachingQuestions = new TeachingQuestions();
        BeanUtils.copyProperties(teachingQuestionsDTO, teachingQuestions);
        teachingQuestionsMapper.add(teachingQuestions);
    }
}
